package org.roger.search;

import java.io.IOException;

import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermRangeQuery;

/**
 * 范围搜索
 * @param field 带查询的域
 * @param start 域的开始
 * @param end 域的结束
 */
public class RangeSearch<T> extends LuceneSearchBase {

	private T start;
	private T end;
	private String field;
	private boolean includeLower;
	private boolean includeUpper;

	public RangeSearch(String field, T start, T end, boolean includeLower, boolean includeUpper) {
		this.field = field;
		this.start = start;
		this.end = end;
		this.includeLower = includeLower;
		this.includeUpper = includeUpper;
	}

	@Override
	public Query getSearchQuery() {

		Query query = null;
		if (start instanceof String) {
			query = TermRangeQuery.newStringRange(field, (String) start, (String) end, includeLower, includeUpper);
		} else if (start instanceof Integer) {
			query = IntPoint.newRangeQuery(field, (Integer) start, (Integer) end);
		} else if (start instanceof Long) {
			query = LongPoint.newRangeQuery(field, (Long) start, (Long) end);
		} else if (start instanceof Float) {
			query = FloatPoint.newRangeQuery(field, (Float) start, (Float) end);
		} else if (start instanceof Double) {
			query = DoublePoint.newRangeQuery(field, (Double) start, (Double) end);
		} else {
			System.out.println("The params type is error!!!");
		}
		System.out.println(query.toString());
		return query;
	}

	public static void main(String[] args) {

		RangeSearch<Integer> search = new RangeSearch<Integer>("id", 1, 3, false, true);
		try {
			search.search("D:/index2");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// RangeSearch<Long> search = new RangeSearch<Long>("modified",1417677720630l,1417678121062l,true,true);
		// try {
		// search.search("D:/index");
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}

	@Override
	public Sort getSort() {
		// TODO Auto-generated method stub
		return null;
	}

}
